const chalk = require("chalk");
const download = require("download-git-repo");
const ora = require("ora");
const spinner = ora("Loading undead unicorns");

/**
 * 克隆目标仓库代码
 * @param {*} gitRemote
 * @param {*} appName
 * @returns
 */
const downloadGit = (gitRemote = "", appName = "") => {
  spinner.start("开始初始化");

  return new Promise((resolve, reject) => {
    download(
      "direct:" + gitRemote,
      appName,
      {
        clone: true,
      },
      (err) => {
        if (err) {
          spinner.fail(chalk.green("初始化失败 \n" + err));
          return reject("拉取模板失败");
        } else {
          spinner.succeed(chalk.green(`初始化成功`));
          return resolve(true);
        }
      }
    );
  });
};

module.exports = {
  downloadGit,
};
